package com.buba.controller.geren;

import com.buba.pojo.User;
import com.buba.service.geren.GerenService;
import com.buba.utils.MyContent;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 赵笛
 */
@RequestMapping("geren")
@Controller
public class GerenController {
    Logger log= LogManager.getLogger();
    @Autowired
    @Qualifier("gerenService")
    GerenService gerenService;

    @RequestMapping("toIndex")
    public String toIndex(){
        return "geren/index";
    }

    /**
     * 登录
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("denglu")
    public String denglu(User user, HttpSession session){
        //调用service方法
        User dlUser=gerenService.findByUsercodeAndPassword(user);
        //判断是否查询到对象
        if(dlUser!=null){
            //不为空，用户名或密码正确，登录成功
            session.setAttribute(MyContent.MSG,"");
            session.setAttribute(MyContent.DATA,dlUser);
            if(dlUser.getRole()==1){
                //四级机构用户
                return "geren/levelForUserMain";
            }else if(dlUser.getRole()==2){
                //中支用户
                return "geren/middleUserMain";
            }else if(dlUser.getRole()==3){
                //省公司用户
                return "geren/provincialCompanyUsersMain";
            }else{
                //系统管理员
                return "geren/administratorMain";

            }

        }else{
            //为空，用户名或密码错误，登录失败
            session.setAttribute(MyContent.MSG,"用户名或密码错误！");
            return "geren/index";
        }
    }

    /**
     * 根据不同权限编号跳转至对应的主页
     * @param role
     * @param username
     * @return
     */
    @RequestMapping("mainpage")
    public String mainpage(Integer role,String username){
        User user=new User();
        user.setRole(role);
        user.setUsername(username);
        if(role==1){
            //四级机构用户
            return "geren/levelForUserMain";
        }else if(role==2){
            //中支用户
            return "geren/middleUserMain";
        }else if(role==3){
            //省公司用户
            return "geren/provincialCompanyUsersMain";
        }else{
            //系统管理员
            return "geren/administratorMain";
        }
    }

    /**
     * 退出登录，返回登录页面
     * @param session
     * @return
     */
    @RequestMapping("exited")
    public String exited(HttpSession session){

        session.removeAttribute(MyContent.MSG);
        session.removeAttribute(MyContent.DATA);
        return "geren/index";
    }

    @RequestMapping("toUpdatePassword")
    public String toUpdatePassword(){
        return "geren/updatePassword";
    }
    /**
     * 修改密码
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("updatePassword")
    public String updatePassword(User user,Model model){
        String oldPassword=user.getOldPassword();
        if(oldPassword.equals(user.getPassword())==false){
            model.addAttribute(MyContent.MSG,"旧密码输入错误");
            return "geren/updatePassword";
        }else{
            if(user.getNewPassword().equals(user.getRePassword())==false){
                model.addAttribute(MyContent.MSG,"两次密码不一致");
                return "geren/updatePassword";
            }else {
                //没写修改密码操作
                User user1=new User();
                user1.setPassword(user.getNewPassword());
                user1.setId(user.getId());
                gerenService.updatePassword(user1);
                return "geren/index";
            }
        }
    }

    /**
     * 修改个人信息
     * @param session
     * @return
     */
    @RequestMapping("toUpdatePersonalInfo")
    public String toUpdatePersonalInfo(HttpSession session){
        User user=(User)session.getAttribute(MyContent.DATA);
        Integer id=user.getId();
        return "geren/updatePersonalInfo";
    }

    /**
     * 修改个人信息
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("updatePersonalInfo")
    public String updatePersonalInfo(User user,HttpSession session){
        Map<String,Object> m=new HashMap<>();
        int i=gerenService.updatePersonalInfo(user);
        if(i>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"修改成功");
            return "geren/index";

        }else{

            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"修改失败");
            User user1=(User)session.getAttribute(MyContent.DATA);
            Integer id=user1.getId();
            return "geren/updatePersonalInfo";
        }

    }

}
